package com.zhanqiao.controller;

import com.alibaba.fastjson.JSON;
import com.zhanqiao.domain.FansBean;
import com.zhanqiao.service.FanService;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FansController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //实现用户的功能

        //设置编码防止乱码
        resp.setCharacterEncoding("utf8");
        resp.setContentType("application/json;charset=utf-8");
        //这里,接收fans.html传递过来的值
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        //首先将数据库中的数据拿出来.分别是id,username,sex,city,signature
        //拿数据调用一个类Service层里面的
        FanService usersService = new FanService();
        try {
            List<FansBean> userBeans = usersService.userService(page, limit);
            Map map = new HashMap<>();
            map.put("code","0");
            map.put("mag","");
            map.put("count","500");
            map.put("data",userBeans);
            //数据封装到bean里面,将这些数据拼接成json字符串传递给fans.html
            String s = JSON.toJSONString(map);
            resp.getWriter().write(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
